package com.luluteam.pushlib.tools;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.luluteam.pushlib.NewsActivity;
import com.luluteam.pushlib.constant.AppHolder;
import com.luluteam.pushlib.model.PushReps;
import com.luluteam.pushlib.model.TagParams;
import com.luluteam.pushlib.model.TargetParams;
import com.luluteam.pushlib.util.StringUtils;
import com.luluteam.pushlib.util.SystemUtils;



import java.util.ArrayList;

/**
 * @author Administrator
 * @date 2018/1/2 0002
 */
public class NotificationBroadcastReceiver extends BroadcastReceiver {

    /**
     * 这个type是为了Notification更新信息的，这个不明白的朋友可以去搜搜，很多
     */
    public static final String TYPE = "type";

    private static final String TAG = "NotificationBroadcast";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int type = intent.getIntExtra(TYPE, -1);
        String messageId = intent.getStringExtra("messageId");


        if (type != -1) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(type);
        }

        PushReps pushReps = new PushReps();
        pushReps.appId = AppHolder.APP_ID;
        pushReps.deviceId = SystemUtils.getInstance(context).getUniqueDeviceID();
        pushReps.messageId = messageId;

        if (action.equals("notification_clicked")) {
            pushReps.status = "open";


            //得到后续目标页面的全类名
            String targetActivity = intent.getStringExtra("targetActivity");
            try {
                Class<?> cls;
                //目标Activity打开
                if (StringUtils.isNotEmpty(targetActivity)) {
                    //通过反射的到后续目标页面的类
                    cls = Class.forName(targetActivity);
                } else {
                    //打开默认Activity
                    cls = NewsActivity.class;
                }

                //得到后续目标页面的参数
                TagParams tagParams = new TagParams();
                ArrayList<TargetParams> targetParams
                        = intent.getParcelableArrayListExtra("targetParams");
                tagParams.setTargetParams(targetParams);
                tagParams.setMessageId(messageId);

                //实例化Intent对象，并设置相关参数
                Intent thisIntent = new Intent(context.getApplicationContext(), cls);
                thisIntent.putExtra(TagParams.INTENT_TAG_PARAMS, tagParams);

                //打开后续目标页面
                context.startActivity(thisIntent);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            Log.i(TAG, "targetActivity==" + targetActivity);

        }

        if (action.equals("notification_cancelled")) {
            //处理滑动清除和点击删除事件
            pushReps.status = "cancel";
        }
        /**
         * 记录对收到的消息的操作信息
         */
        Repository.postPushReps(pushReps);

    }
}

